package com.yzy.soft.system.monitor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Disk {
    private static Logger logger = LoggerFactory.getLogger(Disk.class);

    public DiskInfo getDiskInfo() {
        double diskInfo_FreePartitionSpace_KiloBytes = 0.00D;
        double diskInfo_UsablePatitionSpace_KiloBytes = 0.00D;
        double diskInfo_TotalCapacity_KiloBytes = 0.00D;
        DiskInfo diskInfo = new DiskInfo();
        ZoneDiskInfo zoneDiskInfo = null;
        List<ZoneDiskInfo> zoneDiskInfoList = new ArrayList<>();
        File[] rootFiles = File.listRoots();
        logger.info("磁盘分区:" + rootFiles.length + "个\n");
        diskInfo.setZoneTotalCount(rootFiles.length);

        for (int i = 0; i < rootFiles.length; i++) {
            zoneDiskInfo = new ZoneDiskInfo();
            File file = rootFiles[i];
            getMuliUnitInfo(zoneDiskInfo, file);
            diskInfo_FreePartitionSpace_KiloBytes += zoneDiskInfo.getFreePartitionSpace_KiloBytes();
            diskInfo_UsablePatitionSpace_KiloBytes += zoneDiskInfo.getUsablePatitionSpace_KiloBytes();
            diskInfo_TotalCapacity_KiloBytes += zoneDiskInfo.getTotalCapacity_KiloBytes();
            zoneDiskInfo.setZoneName(file.getPath());
            zoneDiskInfoList.add(zoneDiskInfo);
        }
        diskInfo.setZoneDiskInfoList(zoneDiskInfoList);
        setSubMuliChangeUnitInfo(diskInfo, diskInfo_TotalCapacity_KiloBytes, diskInfo_UsablePatitionSpace_KiloBytes, diskInfo_FreePartitionSpace_KiloBytes,Arith.sub(diskInfo_TotalCapacity_KiloBytes,diskInfo_UsablePatitionSpace_KiloBytes));
        return diskInfo;
    }

    private void getMuliUnitInfo(final SuperDiskInfo diskInfo, File file) {
        long totalCapacitySpace = file.getTotalSpace();
        long freePartitionSpace = file.getFreeSpace();
        long usablePatitionSpace = file.getUsableSpace();
        double usedPatitionSpace = Arith.sub(totalCapacitySpace,usablePatitionSpace);
        logger.info("------- 当前磁盘:" + file.getPath() + "" + file.getName() + "-----------\n");
        logger.info("------- Sizes in Mega Bytes -----------\n");
        setSubMuliChangeUnitInfo(diskInfo, totalCapacitySpace,  usablePatitionSpace,freePartitionSpace,usedPatitionSpace);
    }

    private void setSubMuliChangeUnitInfo(final SuperDiskInfo diskInfo, double totalCapacitySpace, double usablePatitionSpace, double freePartitionSpace,double usedPatitionSpace) {
        logger.info("Total partition size 磁盘空间大小: " + totalCapacitySpace / (1024 * 1024) + " MB");
        logger.info("Usable Space 未使用磁盘空间:: " + usablePatitionSpace / (1024 * 1024) + " MB");
        logger.info("Free Space 可用磁盘空间: " + freePartitionSpace / (1024 * 1024) + " MB");
        logger.info("Used Space 已用磁盘空间: " + usedPatitionSpace / (1024 * 1024) + " MB");
        logger.info("\n**** Sizes in Giga Bytes ****\n");
        logger.info("Total C partition size : " + totalCapacitySpace / (1024 * 1024 * 1024) + " GB");
        logger.info("Usable Space : " + usablePatitionSpace / (1024 * 1024 * 1024) + " GB");
        logger.info("Free Space : " + freePartitionSpace / (1024 * 1024 * 1024) + " GB");
        logger.info("Used Space : " + usedPatitionSpace/ (1024 * 1024 * 1024) + " GB");
        diskInfo.setTotalCapacity_KiloBytes(totalCapacitySpace);
        diskInfo.setFreePartitionSpace_KiloBytes(freePartitionSpace);
        diskInfo.setUsablePatitionSpace_KiloBytes(usablePatitionSpace);
        diskInfo.setUsedPatitionSpace_KiloBytes(usedPatitionSpace);

        diskInfo.setTotalCapacity_MegaBytes(totalCapacitySpace / (1024 * 1024));
        diskInfo.setFreePartitionSpace_MegaBytes(freePartitionSpace / (1024 * 1024));
        diskInfo.setUsablePatitionSpace_MegaBytes(usablePatitionSpace / (1024 * 1024));
        diskInfo.setUsedPatitionSpace_MegaBytes(Arith.div(usedPatitionSpace,(1024 * 1024)));

        diskInfo.setTotalCapacity_GigaBytes(totalCapacitySpace / (1024 * 1024 * 1024));
        diskInfo.setFreePartitionSpace_GigaBytes(freePartitionSpace / (1024 * 1024 * 1024));
        diskInfo.setUsablePatitionSpace_GigaBytes(usablePatitionSpace / (1024 * 1024 * 1024));
        diskInfo.setUsedPatitionSpace_GigaBytes(Arith.div(usedPatitionSpace,(1024 * 1024 * 1024)));
    }

    public static void main(String[] args) {
        new Disk().getDiskInfo();
    }
}
